package com.loves.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loves.admin.config.RabbitMQConfig;
import com.loves.admin.mapper.MsgLogMapper;
import com.loves.admin.mapper.UserMapper;
import com.loves.admin.model.MsgLog;
import com.loves.admin.model.User;
import com.loves.admin.service.UserService;
import com.loves.admin.vo.UserExportVO;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private MsgLogMapper msgLogMapper;

    @Override
    public IPage<User> selectPageVo(Page<User> page,int age) {
        return userMapper.selectPageVo(page,age);
    }

    @Override
    public User initializationUser(String userName,String accountId) {
        User user = new User.Builder().userName(userName).accountId(accountId).createDate(LocalDateTime.now()).createName("系统").build();
        userMapper.insert(user);
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(User user) {
        boolean flag = false;
        user.setCreateDate(LocalDateTime.now());
        user.setCreateName("danielle");
        int count = userMapper.insert(user);
        if(count > 0){
            flag = true;
            send(user);
        }
        return flag;
    }

    private void send(User user){
        Map<String,Object> msgMap = new HashMap<>();
        msgMap.put("username",user.getName());
        //msgMap.put("usermail",user.getUserMail());
        MsgLog msgLog = new MsgLog(JSON.toJSONString(msgMap), RabbitMQConfig.MAIL_EXCHANGE_NAME, RabbitMQConfig.MAIL_ROUTING_KEY_NAME);
        // 消息入库
        msgLogMapper.insert(msgLog);
        CorrelationData correlationData = new CorrelationData(msgLog.getId());
        // 发送消息
        rabbitTemplate.convertAndSend(RabbitMQConfig.MAIL_EXCHANGE_NAME, RabbitMQConfig.MAIL_ROUTING_KEY_NAME, msgMap, correlationData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(User user) {
        boolean flag = false;
        int count = userMapper.updateById(user);
        if(count > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean delete(String id) {
        boolean flag = false;
        int count = userMapper.deleteById(id);
        if(count > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public List<User> findAllList() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getDeleteFlag, false);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserExportVO> findByExport() {
        return userMapper.findByExport();
    }

    @Override
    public User findByAccountId(String accountId){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getAccountId,accountId).eq(User::getDeleteFlag, false);
        return userMapper.selectOne(queryWrapper);
    }
}
